package com.vision.sales.sv.report;

import java.util.HashMap;
import java.util.Map;

import com.vision.sales.cm.data.Month;
import com.vision.sales.cm.db.data.Product;

/**
 * 
 * @author Mark
 *
 */
public class ProductSalesRow implements Comparable<ProductSalesRow> {
	
	private Product product;
	private String productName;
	private Map<String, Double> salesMap;
	
	public ProductSalesRow(Product product, Map<String, Double> salesMap) {
		this.product = product;
		this.salesMap = salesMap;
		
		this.productName = product.getName();
	}
	
	public ProductSalesRow(String product) {
		this.productName = product;
		this.salesMap = new HashMap<String, Double>();
	}

	public Object get(String column) {
		if ("product".equals(column))
			return productName;
		
		return salesMap.get(column);
	}
	
	public Product getProduct() {
		return product;
	}
	
	public String getProductName() {
		return productName;
	}
	
	public Map<String, Double> getSalesMap() {
		if (salesMap == null)
			salesMap = new HashMap<String, Double>();
		return salesMap;
	}
	
	public void computeTotal() {
		double total = 0;
		for (String month : Month.MONTHS) {
			Double value = getSalesMap().get(month);
			if (value != null)
				total += value;
			else
				getSalesMap().put(month, 0d);
		}
		
		getSalesMap().put("total", total);
	}
	
	@Override
	public int compareTo(ProductSalesRow r) {
		return productName.compareToIgnoreCase(r.productName);
	}

}
